<template>
  <div>
    <div v-model="searchData" style="margin: 10px 0">
      <el-input v-model="searchData.bookName" style="width: 200px" placeholder="请输入书名"
                suffix-icon="el-icon-search"></el-input>
      <el-select class="ml-5" v-model="searchData.nickName" @change="changeUser" placeholder="请选择借阅人">
        <el-option
            v-for="item in searchOptions"
            :key="item.id"
            :label="item.nickname"
            :value="item.nickname">
        </el-option>
      </el-select>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="rest">重置</el-button>

    </div>
    <el-button type="danger" @click="delBatch">批量删除 <i class="el-icon-remove-outline"></i></el-button>

    <el-table :data="tableData" border stripe :header-cell-class-name="headerBg" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="borrowId" label="借阅号"></el-table-column>
      <el-table-column prop="bookName" label="书名"></el-table-column>
      <el-table-column prop="nickName" label="借阅人"></el-table-column>
      <el-table-column prop="borrowTime" label="借阅时间"></el-table-column>
      <el-table-column prop="returnTime" label="归还时间"></el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="primary"  @click="del(scope.row,1)">删除 <i class="el-icon-remove-outline"></i></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="current"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "HistoryOder",
  data() {
    return {
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      multipleSelection: [],
      form: {},
      info: {},
      tableData: [],
      searchOptions: [],
      headerBg: 'headerBg',
      current: 0,
      total: 0,
      pageSize: 5,
      dialogFormVisible: false,
      updateFormVisible: false,
      options: [],
      msg: '',
      searchData: {
        bookName: '',
        nickName: '',
        name: ''
      }
    }
  },
  created() {
    this.load()
    this.getSearchOption()
  },
  methods: {
    getSearchOption() {
      this.request.get("/history/getUserOptions").then(res => {
        this.searchOptions = res.data
      })
    },
    changeUser() {
      this.load()
    },
    rest() { //重置搜索栏
      this.searchData.nickName = ''
      this.searchData.bookName = ''
      this.load()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
      console.log(this.multipleSelection.map(v => v.id))
    },

    del(row) {
        this.$confirm('确定删除吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.request.delete("/history/delete/" + row.id+"/"+2).then(resp => {
            if (resp.code === 200) {
              this.load()
              this.$message({
                type: 'success',
                message: '取消删除!',
              });
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          });
        });
    },
    addHander() {
      this.dialogFormVisible = true
      this.form = {}
    },
    handleSizeChange(pageSize) {
      console.log("每页条数: " + pageSize)
      this.pageSize = pageSize;
      this.load()
    },
    handleCurrentChange(current) {
      console.log("当前页码: " + current)
      this.current = current;
      this.load()
    },
    collapse() {  // 点击收缩按钮触发
      this.isCollapse = !this.isCollapse
      if (this.isCollapse) {  // 收缩
        this.sideWidth = 64
        this.collapseBtnClass = 'el-icon-s-unfold'
        this.logoTextShow = false
      } else {   // 展开
        this.sideWidth = 200
        this.collapseBtnClass = 'el-icon-s-fold'
        this.logoTextShow = true
      }
    },
    load() {
      this.request.get("/history/page", {
            params: {
              current: this.current,
              pageSize: this.pageSize,
              bookName: this.searchData.bookName,
              nickName: this.searchData.nickName
            }
          }
      ).then(resp => {
        console.log(resp)
        this.tableData = resp.data.records
        this.total = resp.data.total
      })
    },

    delBatch(){
      let ids =  this.multipleSelection.map(v=>v.id)  //[1,2,3]
      this.$confirm('此操作将删除这些用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.request.delete("/history/delete/batch/"+2+"/"+ids).then(resp=>{
          if (resp.code===200){
            this.load()
            this.$message({
              type: 'success',
              message: '删除成功!',
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },


  }
}
</script>

<style>
.headerBg {
  background: #eee !important;
}
</style>